<?php

namespace IDock\Insurance\JDAL\Response;

use IDock\Http\Response;

class AllJDALResponse extends Response
{
    public function resolve(): Response
    {
        
        $isTestResponse = $GLOBALS['testResponse'] ?? '';
        
        $body = $this->getBody();

        if (!json_decode($body)) {
            $this->setError('返回数据异常');
            return $this;
        }
        $data = json_decode($body, true);
        
        if ($isTestResponse) {
            $this->setBody($data);
            $this->setResult($data);

            if ($this->printer and is_string($this->printer)) {
                $this->printer = new $this->printer;
            }
            return $this;
        }

        $resultCode = $data['code'] ?? '';
        $resultMess = $data['desc'] ?? '';
        $success = $data['success'] ?? '';

        if ($resultCode != '0000') {
            $this->setError($resultMess);
            return $this;
        }

        if (!$success) {
            $this->setError('京东异常数据');
            return $this;
        }

        $this->setBody($data);

        if ($this->printer and is_string($this->printer)) {
            $this->printer = new $this->printer;
        }

        return $this;
    }

    public function setResult($body)
    {
        parent::setBody($body);
    }
}
